-- 周末好礼定期邮件
local mis_uu = require('modules.commhm.utils')
local uu = mis_uu
local mis_timer = require('modules.commhm.ngxTimerWrap')
local os_date = os.date

local ngx = ngx

local mis_log = nil
local mis_config = nil
local mis_data = nil
local mis_common = nil
local mis_bridge = nil
local mis_calcu = nil
local t_retCode = nil

--------------------------------------------------------------
local mis_weekendMail = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_weekendMail

--------------------------------------------------------------
-- 说明：定期发邮件
local function send_mail_loop()
	local n_code, s_desc
	local n_now = mis_data.weekend_gift_now()
	local n_hour = tonumber(os_date("%H", n_now))
	local n_min = tonumber(os_date("%M", n_now))
	local n_weekday = tonumber(os_date("%w", n_now))
	local s_nowMin = os_date("%Y%m%d %H:%M", n_now)
	local t_conf = mis_data.t_weekend_gift.t_conf
	local t_mail = mis_data.t_weekend_gift.t_conf.t_mail

	repeat
		if not t_conf or mis_data.t_weekend_gift.n_status ~= mis_config.t_workStatus.n_ENABLE then
			mis_log.day('weekendMail', 'weekend gift not enable! config: ' .. uu.to_str(t_conf))
			break
		end

		if t_conf.n_startTime and n_now < t_conf.n_startTime then
			mis_log.day('weekendMail', 'weekend gift not start!')
			break
		end

		if t_conf.n_endTime and t_conf.n_endTime < n_now then
			mis_log.day('weekendMail', 'weekend gift out of time!')
			break
		end

		if t_mail.n_send ~= 1 then
			mis_log.day('weekendMail', 'weekend gift mail send closed by config! send: ' .. uu.to_str(t_mail.n_send))
			break
		end

		if (t_mail.n_send_weekday % 7) ~= (n_weekday % 7) then
			mis_log.day('weekendMail', 'weekend gift weekday not match: config weekday: ' .. uu.to_str(t_mail.n_send_weekday) .. ", now weekday: " .. n_weekday)
			break
		end

		if t_mail.t_send_time.hour ~= n_hour or t_mail.t_send_time.min ~= n_min then
			mis_log.day('weekendMail', 'weekend gift mail time not match! now: ' .. n_now)
			break
		end

		if not t_mail.s_title or not t_mail.s_content then
			mis_log.day('weekendMail', 'weekend gift mail content error: ' .. uu.to_str(t_mail))
			break
		end

		-- 防止多发
		if ngx.weekendMailLastSendTime ~= s_nowMin then
			ngx.weekendMailLastSendTime = s_nowMin
			-- 发送
			n_code, s_desc = mis_bridge.send_sys_email(
				t_mail.s_title,
				t_mail.s_content,
				'',
				mis_config.n_weekendMailValidTIme,
				nil,
				t_mail.n_jumpTo
			)
			if n_code ~= t_retCode.n_OK then
				mis_log.day('weekendMail', 'send error! code: ' .. n_code .. ", msg: ".. uu.to_str(s_desc))
				break
			else
				mis_log.day('weekendMail', 'send weekend gift mail ok!')
			end
		end
	until true

	mis_log.day('weekendMail', 'weekend gift mail loop! now: ' .. n_now)

	mis_timer:new():ngx_timer_at(mis_config.n_weekendMailInterval, send_mail_loop)
end

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_log = t_moddules.mis_log
	mis_config = t_moddules.mis_config
	mis_data = t_moddules.mis_data
	mis_common = t_moddules.mis_common
	mis_bridge = t_moddules.mis_bridge
	mis_calcu = t_moddules.mis_calcu
	t_retCode = mis_config.t_retCode
end

--------------------------------------------------------------
-- 说明：load级初始化
this.init_load = function()
	mis_log.day('weekendMail', 'init_load! worker ' .. ngx.worker.id())
	-- if mis_calcu.is_master_server() == true then
	if mis_config.n_debug == 1 then
		-- 开启循环
		if ngx.worker.id() == 0 then
			local _, s_err = mis_timer:new():ngx_timer_at(mis_config.n_weekendMailInterval, send_mail_loop)
			if s_err then
				return t_retCode.n_SYS_ERR, 'start ngx.timer.every failed, ' .. s_err
			else
				mis_log.day('weekendMail', 'start!')
			end
		end
	end
	return t_retCode.n_OK
end

return this